package com.roc.springboot.producer;

import com.roc.entity.Order;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 描述: 订单发送消息服务
 * 包名: com.roc.springboot.producer
 * 作者: ChenGuop.
 * 日期: 2018/11/29 下午4:35.
 * 项目名称: springboot-rabbit
 * 版本: 1.0
 * JDK: since 1.8
 */
@Component
public class OrderSender {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送消息
     * @param order
     * @throws Exception
     */
    public void send(Order order)throws Exception{
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                correlationData.getId();
                //成功
                if (b){
                    // 修改msg状态
                } else {
                    System.out.println("消息处理，接受失败！");
                }
            }
        });
        CorrelationData correlationData = new CorrelationData();
        correlationData.setId(order.getId());
        rabbitTemplate.convertAndSend("order-exchange", //exchange 交换机
                "order.abc", // routingKey 路由
                order, // msg 消息
                correlationData); //消息唯一Id
    }

}
